Skip to content

Instantly share code, notes, and snippets.

@IlmariKu
Created April 2, 2024 11:31
Show Gist options
  • Save IlmariKu/1a86432477e0771810ae38c0ade04336 to your computer and use it in GitHub Desktop.
Save IlmariKu/1a86432477e0771810ae38c0ade04336 to your computer and use it in GitHub Desktop.
Henkilötunnus 2023 regex - new finnish social security number regex to identify
// Henkilötunnus 2023 regex - new finnish social security, regex to identify potential numbers
// Does not actually validate using the checksum
// Allows for case-insensitivity using the /i in the end.
// Includes test variables and tests, just copy the old-form regex or new one
const oldSsnRegex = /^(\d{6}[-+A]\d{3}\S{1})$/i;
/** Compatible with old-form */
const newSsnRegex = /^(\d{6}[a-zA-Z]\d{3}\S{1})$/i;
/** Old-form social security numbers
* Generated using https://www.lintukoto.net/muut/henkilotunnus/
* Note: Could be, by random chance, actual SSN's
*/
const oldSocialSecurityNumbers = ['080575-7270', '200271-500Y', '210152-804P', '010552-8716', '060245-337Y', '010686-629Y', '091188-382P', '020504A902E'];
/** Test-set of imaginary new social-security numbers
* Incompatible with new SsnRegex
Source from Digi- ja Väestötietovirasto https://dvv.fi/hetu-uudistus
*/
const newSocialSecurityNumbers = [
'010594Y9032',
'010594Y9021',
'020594X903P',
'020594X902N',
'030594W903B',
'030694W9024',
'040594V9030',
'040594V902Y',
'050594U903M',
'050594U902L',
'010516B903X',
'010516B902W',
'020516C903K',
'020516C902J',
'030516D9037',
'030516D9026',
'010501E9032',
'020502E902X',
'020503F9037',
'020504B904H',
];
describe('Finnish SSN, simple-regex to detect, does not validate', () => {
// Invalid SSNs for testing purpose
const invalidSSNs = ['123-456-789', '0000-00-0000', 'invalid-ssn', '200271-500YZfsdfsfes', '324142-AFBS', '200271-500YZ', '200271-11500YZ', '120345-33AY', '120345333AY', '1203459331Y'];
oldSocialSecurityNumbers.forEach((ssn) => {
it(`validates old-format SSN: ${ssn}`, () => {
expect(oldSsnRegex.test(ssn)).toBe(true);
});
});
newSocialSecurityNumbers.forEach((ssn) => {
it(`validates new-format SSN: ${ssn}`, () => {
expect(newSsnRegex.test(ssn)).toBeTruthy();
});
});
newSocialSecurityNumbers.forEach((ssn) => {
it(`invalid new SSN vs. old Regex: ${ssn}`, () => {
expect(oldSsnRegex.test(ssn)).toBeFalsy();
});
});
invalidSSNs.forEach((ssn) => {
it(`invalidates old SSN: ${ssn}`, () => {
expect(oldSsnRegex.test(ssn)).toBeFalsy();
});
});
invalidSSNs.forEach((ssn) => {
it(`invalidates old SSNs, using the new regex: ${ssn}`, () => {
expect(newSsnRegex.test(ssn)).toBeFalsy();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment